/******************************************************************************
 * Copyright (C) 2016  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_SYSTEM_MUTEX_POSIX_H_
#define NODE_SYSTEM_MUTEX_POSIX_H_

#include <pthread.h>
#include <node/noncopyable.h>

namespace node {
	namespace system {

	class mutex_impl : private noncopyable
	{
	public:
		mutex_impl(void);
		~mutex_impl(void);

		bool lock_impl(void);
		bool trylock_impl(void);
		void unlock_impl(void);

	private:
		pthread_mutex_t _mutex;
	};

	inline mutex_impl::mutex_impl(void)
	{
		_mutex = PTHREAD_MUTEX_INITIALIZER;
	}
	inline mutex_impl::~mutex_impl(void)
	{
		pthread_mutex_destroy(&_mutex);
	}

	inline bool mutex_impl::lock_impl(void)
	{
		return (pthread_mutex_lock(&_mutex) == 0);
	}
	inline bool mutex_impl::trylock_impl(void)
	{
		return (pthread_mutex_trylock(&_mutex) == 0);
	}
	inline void mutex_impl::unlock_impl(void)
	{
		pthread_mutex_unlock(&_mutex);
	}

}}

#endif // NODE_SYSTEM_MUTEX_POSIX_H_